import * as mutations from './mutation-types'
import env from '../../env'
import jsapi from '../lib/ddApiConfig'
import {getRequestId, S, U} from '../lib/backend'
import axios from 'axios'

export default {

    getRequestAuthCode({ dispatch, state }, corpId) {

    if(!window.ability || window.ability < jsapi['runtime.permission.requestAuthCode']){
      console.warn('容器版本过低，不支持 runtime.permission.requestAuthCode')
      return;
    }

    dd.runtime.permission.requestAuthCode({
      corpId : state.app.ddConfig.corpId || corpId,
      onSuccess : function(result) {
        dispatch(mutations.UPDATE_CODE,result.code)

        getUserInfo({ dispatch, state },result.code)

        console.log('获取到了免登陆code=>'+result.code)
      },
      onFail : function(err) {
        dispatch(mutations.UPDATE_CODE,false)
        console.log('获取免登陆code失败')
      }
    });
  },

  // getUserInfo({dispatch, state}, code) {
  //   axios.get(env.API_HOST+'/user/getUserinfo', {
  //     params: {
  //       code: code,
  //       corpId: state.app.ddConfig.corpId,
  //       suiteKey: window.getParamByName('suiteKey')
  //     },
  //     timeout: 5000,
  //   }).then(function (response) {
  //     if(response.status == 200 && response.data.code == 200){
  //       let user = response.data.result
  //       dispatch(mutations.LOGIN_SUCCESS, user);
  //       dispatch(mutations.UPDATE_SYS_LEVEL, user.sys_level);
  //     }else{
  //       dispatch(mutations.LOGIN_ERROR,false)
  //     }
  //   }).catch(function (err) {
  //     dispatch(mutations.LOGIN_ERROR,false)
  //   });
  // },


  Login({commit, state}, requestId, code) {

    return axios.post(env.CRM_API_HOST+'/ding/login', S({
      params: {
        "trace": {
        "requestId": requestId
        },
        "info": {
        "code": code
        }
      },
      timeout: 5000,
    })).then(function (response) {

      if(response.data.errcode == 0){
        let token = response.token;
        commit(mutations.UPDATE_TOKEN, token);
      }else{
        commit(mutations.LOGIN_ERROR,false)
      }

    }).catch(function (err) {

      commit(mutations.LOGIN_ERROR,false)

    });
  },

  //获取用户信息
  getUserInfo({dispatch, commit, state}) {
    if(!state.token) {
      dispatch('Login').then(() => {
        dispatch('getUserInfo');
      })
    } else {
      axios.get(env.CRM_API_HOST+'/users/'+state.token)
      .then(function(response){
        commit(mutations.UPDATE_USER, user);
      })
    }
  },

  //获取工作台信息
  saveWorckbenchInfo({dispatch, commit, state}, info) {
    let params = S({
      "trace": { "requestId": getRequestId() },
      "achievement":info }
    );
    console.log(params)
    return axios.post(env.CRM_API_HOST+'/users/'+state.token+'/achievements', params);
  },

  getWorckbenchInfo({dispatch, commit, state}, info) {
    return axios.get(env.CRM_API_HOST+'/users/'+state.token+'/index?'+U(info));
  },

  getAchievementsInfo({dispatch, commit, state}, year) {
    return axios.get(env.CRM_API_HOST+'/users/'+state.token+'/achievements/'+year);
  },

  //查询客户信息列表接口，包括社交客户和普通客户
  getCustoms({dispatch, commit, state}, data) {
    let params = U(data);
    console.debug(env.CRM_API_HOST+'/users/'+state.token+'/customs?'+params)
    return axios.get(env.CRM_API_HOST+'/users/'+state.token+'/customs?'+params);
  },

  getActivities({dispatch, commit, state}, data) {
    let params = U(data);
    return axios.get(env.CRM_API_HOST+'/users/'+state.token+'/h5');
  },

  //查询客户公海信息列表接口，包括社交客户和普通客户
  getClientSeasCustoms({dispatch, commit, state}, data) {
    return axios.get(env.CRM_API_HOST+'/users/'+state.token+'/gonghai?'+U(data));
  },

  //查询详情信息，包括社交客户和普通客户
  getCustomInfoById({dispatch, commit, state}, id) {
    return axios.get(env.CRM_API_HOST+'/users/'+state.token+'/customs/'+id);
  },

  //管理客户信息接口
  saveClientInfo({dispatch, commit, state}, info) {
    let id = info.id
    delete info.id
    return axios.post(env.CRM_API_HOST+'/users/'+state.token+'/customs/'+id, S({
      "trace": { "requestId": getRequestId() },
      "info":info }
    )
    );
  },

  //查询客户宝宝列表接口
  getBabys({dispatch, commit, state}, id){
    return axios.get(env.CRM_API_HOST+'/users/'+state.token+'/customs/'+id+'/babys');
  },

  //管理客户宝宝接口
  saveBabys({dispatch, commit, state}, info) {
    return axios.post(env.CRM_API_HOST+'/users/'+state.token+'/customs/'+id+'babys', S({
    "trace": { "requestId": getRequestId() },
    "info":info
    }));
  },

  //查询线索列表
  getClues({dispatch, commit, state}, data){
    return axios.get(env.CRM_API_HOST+'/users/'+state.token+'/clues?'+U(data));
  },

  //保存线索列表
  saveClues({dispatch, commit, state}, info){
    return axios.post(env.CRM_API_HOST+'/users/'+state.token+'/clues', S({
    "trace": { "requestId": getRequestId() },
    "info":info
    }));
  },

  //下发h5
  sendH5({dispatch, commit, state}, info){
    return axios.post(env.CRM_API_HOST+'/users/'+state.token+'/sendh5', S({
      "trace": { "requestId": getRequestId() },
      "info":info
    }));
  },

  //下发h5
  sendCard({dispatch, commit, state}, info){
    return axios.post(env.CRM_API_HOST+'/users/'+state.token+'/send_card', S({
      "trace": { "requestId": getRequestId() },
      "info":info
    }));
  },

  //新增预约试听信息
  saveAppointments({dispatch, commit, state}, info){
    console.debug(info.assist_suid,'info.assist_suid')
    return axios.post(env.CRM_API_HOST+'/users/'+state.token+'/appointments', S({
    "trace": { "requestId": getRequestId() },
    "info":info
    }));
  },

  //查询团队列表
  getTeams({dispatch, commit, state}, data){
    let args = U(data);
    return axios.get(env.CRM_API_HOST+'/users/'+state.token+'/teams?'+args);
  },

  //保存团队
  saveTeams({dispatch, commit, state}, info){

    let params = ''
    if (typeof info.id !== undefined) {
      params = '/'+info.id
    }

    return axios.post(env.CRM_API_HOST+'/users/'+state.token+'/teams'+params, S({
      "trace": {
        "requestId": getRequestId()
      },
      "info": info
    }));
  },

  //查询团队
  getTeam({dispatch, commit, state}, teamId){
    return axios.get(env.CRM_API_HOST+'/users/'+state.token+'/teams/'+teamId);
  },

  //名片信息查询接口
  getCard({dispatch, commit, state}){
    return axios.get(env.CRM_API_HOST+'/users/'+state.token+'/card');
  },

  //名片信息查询接口
  getDictionary({dispatch, commit, state}, code){
    let params = ''
    if(code) {
      params = '/'+code
    }
    return axios.get(env.CRM_API_HOST+'/data_dictionary'+params);
  },

  //名片信息保存接口
  saveCard({dispatch, commit, state}, info){

    let data =  Object.assign({"trace": {"requestId": getRequestId()}}, info);

    return axios.post(env.CRM_API_HOST+'/users/'+state.token+'/card', S(data));
  },

  //客户调配
  distribute({dispatch, commit, state}, info){
    let data =  Object.assign({"trace": {"requestId": getRequestId()}}, info);
    return axios.post(env.CRM_API_HOST+'/users/'+state.token+'/distribute', S(data));
  },

  //测试actions
  actionA({dispatch, commit, state}){
    commit(mutations.UPDATE_TOKEN, 'sss');
    dispatch('actionB', 'a')
  },

  //测试actions
  actionB({dispatch, commit, state}, params){
    console.log('test', params)
  },

}
